﻿using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Caliburn.MusicPlayer.UserControls
{
    /// <summary>
    /// PlayPanel.xaml 的交互逻辑
    /// </summary>
    public partial class PlayPanel : UserControl
    {
        public PlayPanel()
        {
            InitializeComponent();
        }

        public RelayCommand PlayPreviousCommand
        {
            get { return (RelayCommand)GetValue(PlayPreviousCommandProperty); }
            set { SetValue(PlayPreviousCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PlayPreviousCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PlayPreviousCommandProperty =
            DependencyProperty.Register("PlayPreviousCommand", typeof(RelayCommand), typeof(PlayPanel), new PropertyMetadata(null));


        public RelayCommand Play_StopCommand
        {
            get { return (RelayCommand)GetValue(Play_StopCommandProperty); }
            set { SetValue(Play_StopCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Play_StopCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Play_StopCommandProperty =
            DependencyProperty.Register("Play_StopCommand", typeof(RelayCommand), typeof(PlayPanel), new PropertyMetadata(null));


        public RelayCommand Play_NextCommand
        {
            get { return (RelayCommand)GetValue(Play_NextCommandProperty); }
            set { SetValue(Play_NextCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Play_NextCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Play_NextCommandProperty =
            DependencyProperty.Register("Play_NextCommand", typeof(RelayCommand), typeof(PlayPanel), new PropertyMetadata(null));

        public double PlayProgress
        {
            get { return (double)GetValue(PlayProgressProperty); }
            set { SetValue(PlayProgressProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PlayProgress.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PlayProgressProperty =
            DependencyProperty.Register("PlayProgress", typeof(double), typeof(PlayPanel), new PropertyMetadata(OnPlayProgressChanged));

        private static void OnPlayProgressChanged(object sender,DependencyPropertyChangedEventArgs e)
        {
            PlayPanel panel = (PlayPanel)sender;
            panel.progress_bar.Value = (double)e.NewValue;
        }


        public event RoutedEventHandler VolumeChange
        {
            add { AddHandler(VolumeChangeEvent, value); }
            remove { RemoveHandler(VolumeChangeEvent, value); }
        }

        public static readonly RoutedEvent VolumeChangeEvent = EventManager.RegisterRoutedEvent(
        "VolumeChange", RoutingStrategy.Bubble, typeof(EventHandler<VolumeChangedEventArgs>), typeof(PlayPanel));



        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            VolumeChangedEventArgs args = new VolumeChangedEventArgs(VolumeChangeEvent,this);
            args.Volume = e.NewValue;
            RaiseEvent(args);
        }

        private void btn_Play_Previous_Click(object sender, RoutedEventArgs e)
        {
            PlayPreviousCommand?.Execute(null);
        }

        private void btn_play_stop_Click(object sender, RoutedEventArgs e)
        {
            Play_StopCommand?.Execute(null);
        }

        private void btn_Next_play_Click(object sender, RoutedEventArgs e)
        {
            Play_NextCommand?.Execute(null);
        }
    }

    public class VolumeChangedEventArgs:RoutedEventArgs
    {
        public double Volume { get; set; }

        public VolumeChangedEventArgs(RoutedEvent events,object source):base(events,source) 
        {

        }
    }
}
